import hashlib
import os
import shutil
from tqdm import tqdm
import imghdr


def main():
    zone1 = open("000000000.jpg","rb")
    zone1_md5 = hashlib.md5(zone1.read()).hexdigest()

    zone2 = open("000000000.gif","rb")
    zone2_md5 = hashlib.md5(zone2.read()).hexdigest()

    zone3 = open("000000001.gif", "rb")
    zone3_md5 = hashlib.md5(zone3.read()).hexdigest()

    zone4 = open("000000002.jpg", "rb")
    zone4_md5 = hashlib.md5(zone4.read()).hexdigest()

    # 这三张是失效图片
    zone_arr = [zone1_md5, zone2_md5, zone3_md5, zone4_md5]

    path = "F:\BeautifulPictures\\3"  # 文件夹目录
    files = os.listdir(path)  # 得到文件夹下的所有文件名称
    for file in tqdm(files):
        file_path = path + "\\" + file
        # 如果是损坏文件
        if imghdr.what(file_path) is None:
            # 移入临时文件夹中,因为这个方法也会有误判  所以没有直接删除
            shutil.move(file_path, "F:\\temp\\")
            continue
        f = open(file_path, "rb")  # 打开文件
        md5 = hashlib.md5(f.read()).hexdigest()
        f.close()
        for zone in zone_arr:
            if zone == md5:
                shutil.move(file_path, "F:\\temp\\")


if __name__ == '__main__':
    main()